package chatTest;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class Receive implements Runnable {

	private DataInputStream dis;
	private boolean IsRunning = true;

	public Receive() {
	}

	public Receive(Socket client) {
		try {
			dis = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			IsRunning = false;
			Close.closeAll(dis);
		}
	}

	/**
	 * 接受數據
	 * 
	 * @return
	 */
	public String receive() {
		String msg = "";
		try {
			msg = dis.readUTF();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			IsRunning = false;
			Close.closeAll(dis);
		}
		return msg;
	}

	@Override
	public void run() {
		for (; IsRunning;) {
			System.out.println(receive());
		}
	}

}